import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * qq邮箱授权码：hazqeinjvixnbaha
 */

public class Email1 {
    //授权码
    private static String password = "hazqeinjvixnbaha";
    //发送人的qq
    private static String myqq = "1253969907";
    //收件人的qq
    private static String userqq = "1829917331";
    //标题
    private static String title = "测试验证码";
    //要输入的内容
    private static String textContext = "测试验证码";

    public static void main (String[] args) throws Exception  {
        title = "蔡导晚上好";
        textContext = "没错，我就是蔡导，我是演员哈哈";
        //使用lamda表达式,
        // 可以消息轰炸，哈哈，有点狠
        new Thread(()->{
            for (int i = 0; i < 1; i++) {
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
                String datetime = sdf.format(date);
                try {
                    //调用方法测试
                    sendEmail(myqq,userqq,title,textContext,datetime);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        System.out.println("发送成功！");
    }

    public static void sendEmail(String myqq, String userqq , String title, String textContext,String time) throws Exception{
        Properties properties = new Properties();
        //设置qq邮箱的服务器
        properties.setProperty("mail.host","smtp.qq.com");
        //邮件发送协议
        properties.setProperty("mail.transport.protocol","smtp");
        //验证用户名以及邮箱授权码
        properties.setProperty("mail.smtp.auth","true");

        //关于qq邮箱，还要设置SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable","true");
        properties.put("mail.smtp.ssl.socketFactory",sf);

        //开始发送邮件
        //创建定义整个应用程序所需的环境信息的session

        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication( myqq + "@qq.com",password);
            }
        });
        //开启session，查看程序的运行状态
        session.setDebug(true);
        //通过session得到transport对象
        Transport ts = session.getTransport();

        //使用邮箱的用户名和授权码连上邮箱服务器
        ts.connect("smtp.qq.com",myqq + "@qq.com",password);
        //创建邮件
        MimeMessage mimeMessage = new MimeMessage(session);
        //指明邮件的发件人
        mimeMessage.setFrom(new InternetAddress(myqq + "@qq.com"));
        //指明邮件的收件人
        mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress( userqq + "@qq.com"));
        //邮件的标题
        mimeMessage.setSubject(title);
        //邮件的文本内容
        mimeMessage.setContent(textContext + time,"text/html;charset=utf-8");
        //发送邮件
        ts.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
        //关闭连接
        ts.close();
    }
}
